﻿using System;

using System.Collections.Generic;
using System.Text;
using Microsoft.WindowsMobile.Samples.Location;

namespace PublicTransport.Services
{
    /// <summary>
    /// Trieda spravujúca GPS. Singleton.
    /// </summary>
    class LocationService : ILocationService
    {

        private static LocationService instance;
        public static LocationService Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new LocationService();
                }
                return instance;
            }
        }


        /// <summary> Event v prípade zmeny lokácie </summary>
        public event Microsoft.WindowsMobile.Samples.Location.LocationChangedEventHandler LocationChanged;
        
        /// <summary> Aktuálna pozícia </summary>
        private GpsPosition position = null;

        /// <summary> Trieda spravujúca GPS </summary>
        private Gps gps = null;

        /// <summary>   Vytvorí inštanciu GPS triedy a uloží si referenciu </summary>
        private LocationService()
        {
            gps = new Gps();
            gps.LocationChanged += new LocationChangedEventHandler(gps_LocationChanged);
        }

        void gps_LocationChanged(object sender, LocationChangedEventArgs args)
        {
            if (LocationChanged != null)
                LocationChanged(sender, args);
        }

        /// <summary>   Zapnúť GPS a začať vyvolávať event LocationChanged. </summary>
        public void Start()
        {
            if (!gps.Opened)
            {
                gps.Open();
            }
        }

        /// <summary>   Zastaviť GPS - nutné zavolať čo najskôr, kvôli veľkej spotrebe energie. </summary>
        public void Stop()
        {
            if (gps.Opened)
            {
                gps.Close();
            }
        }


    }
}
